//
//  OpinionController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/4/27.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

//意见反馈
class OpinionController: BaseController {
    var placeholderLabel = UILabel()
    var emailText = UITextField()
    var textview = UITextView()
    override func viewDidLoad(){
        super.viewDidLoad()
        setupNavBar(title: "意见反馈", isback: true, appearWay: 2, isright: false)//自定义导航
        self.view.backgroundColor = tableBgColor
        textview = UITextView(frame:CGRect(x:10, y:kNavigationBarH + kStatusBarH + 10, width:kScreenW - 20, height:150))
        textview.layer.cornerRadius = 5
        textview.delegate = self
        textview.inputAccessoryView = AddToolBar()
        self.view.addSubview(textview)
        
        self.placeholderLabel = UILabel.init() // placeholderLabel是全局属性
        self.placeholderLabel.frame = CGRect(x:5, y:0, width:kScreenW - 20, height:30)
        self.placeholderLabel.font = UIFont.systemFont(ofSize:14)
        self.placeholderLabel.text = "请输入您遇见的问题或告诉我们您宝贵的意见"
        self.placeholderLabel.textColor = placeholderColor
        textview.addSubview(placeholderLabel)
    
        let emailView = UIView.init(frame: CGRect(x: 10, y: kNavigationBarH + kStatusBarH + 170, width: kScreenW - 20, height: 40))
        emailView.backgroundColor = UIColor.white
        emailView.layer.cornerRadius = 5
        let email_left = UILabel(frame: CGRect(x: 10, y: 0, width: 60, height: 40))
        email_left.text = "联系我们"
        email_left.font = UIFont.systemFont(ofSize: 14)
        emailView.addSubview(email_left)
        
        emailText = UITextField(frame: CGRect(x: 80, y: 0, width: kScreenW - 70, height: 40))
        emailText.placeholder = "请输入您的邮箱"
        emailText.font = UIFont.systemFont(ofSize: 14)
        emailText.inputAccessoryView = AddToolBar()
        emailView.addSubview(emailText)
        
        self.view.addSubview(emailView)
        
        //确定按钮
        let button:UIButton = UIButton(type:.custom)
        button.frame = CGRect(x:10, y:kNavigationBarH + kStatusBarH + 220, width:kScreenW - 20, height:40)//设置登陆按钮位置和大小
        button.layer.cornerRadius = 10;
        button.backgroundColor = appMainColor
        button.showsTouchWhenHighlighted = true //
        button.adjustsImageWhenHighlighted=false ////使触摸模式下按钮也不会变暗（半透明
        button.setTitle("确定", for:.normal)
        button.addTarget(self, action: #selector(opinionBtnClick), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
        
    }
}
extension OpinionController {
    //意见反馈
    func opinionBtnClick() {
        if "" == textview.text! {
            checkAlertFun(message: "请输入意见内容！")
        }else{
           opinionFunc()
        }
    }
    //提交意见
    func opinionFunc() {
        if reachability.isReachable {
            let parameter:[String:Any] = [
                "content":textview.text!,
                "email":emailText.text!
            ]
            Alamofire.request("\(projectUrl)/user/feedback/add/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)",method:.post, parameters:parameter,headers:headers).responseJSON { (response) in
                let jsonString = response.result.value!
                let json = JSON(jsonString)
                if json["success"] == true {
                    let alertController = UIAlertController(title: alertTitle, message: "意见反馈提交成功！", preferredStyle: .alert)
                    let okAction = UIAlertAction(title: "确定", style: .default, handler:{
                        (UIAlertAction) -> Void in
                        self.navigationController?.popViewController(animated: true)
                    })
                    alertController.addAction(okAction)
                    self.present(alertController, animated: true, completion: nil)
                }else{
                    NSLog("意见反馈出错啦!\(json["rdata"])")
                    self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                }
            }
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
extension OpinionController:UITextViewDelegate{
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        self.placeholderLabel.isHidden = true // 隐藏
        return true
    }
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            self.placeholderLabel.isHidden = false  // 显示
        }
        else{
            self.placeholderLabel.isHidden = true  // 隐藏
        }
    }
}
